package com.diting.library.cmcc.activity;

import static com.diting.library.cmcc.util.CmicSSOHelper.PERMISSIONS_REQUEST_READ_PHONE_STATE_DISPLAY_LOGIN;
import static com.diting.library.cmcc.util.CmicSSOHelper.PERMISSIONS_REQUEST_READ_PHONE_STATE_IMPLICIT_LOGIN;
import static com.diting.library.cmcc.util.CmicSSOHelper.PERMISSIONS_REQUEST_READ_PHONE_STATE_PRE;
import static com.diting.library.cmcc.util.CmicSSOHelper.RESULT;
import static com.diting.library.cmcc.util.CmicSSOHelper.RESULT_OF_SIM_INFO;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.diting.library.cmcc.R;
import com.diting.library.cmcc.util.CmicSSOHelper;
import com.diting.library.cmcc.util.StringFormat;
import com.diting.library.cmcc.util.VerifyHelper;

import java.lang.ref.WeakReference;


public class SimpleActivity extends Activity implements View.OnClickListener {

    protected String TAG = "MainActivity";
    //    private static final int RESULT = 0x111;
//    protected static final int RESULT_OF_SIM_INFO = 0x222;
    private Context mContext;
    public String mResultString;
    private TextView phoneEt;
    public ResultDialog mResultDialog;
//    boolean isShowResult = true;
//    private CmicSSOHelper ssoHelper;
    private VerifyHelper verifyHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
//        initSDK();
//        ssoHelper = new CmicSSOHelper();
//        ssoHelper.initSDK(this, new CmicSSOHelper.SSOResultListener() {
//            @Override
//            public void onResult(String result, int delay) {
//                mResultString = result;
//                if (delay > 0) {
//                    mHandler.sendEmptyMessageDelayed(RESULT, delay);
//                } else {
//                    mHandler.sendEmptyMessage(RESULT);
//                }
//            }
//
//            @Override
//            public void onSimInfo(String info) {
//
//            }
//        });
        verifyHelper = new VerifyHelper();
        verifyHelper.initSDK(this, new VerifyHelper.OnTokenResultListener() {
            @Override
            public void onTokenSuccess(String result, int delay) {

            }

            @Override
            public void onTokenFailed(String info) {

            }
        });
        verifyHelper.setAuthUIConfig();
        init();
    }

    @Override
    protected void onDestroy() {
//        mGenAuthnHelper.deleteAuthThemeConfig();
//        mGenAuthnHelper.setPageInListener(null);
//        mListener = null;
        verifyHelper.onDestroy();
        super.onDestroy();
    }

    private void init() {
        setContentView(R.layout.activity_main);
        findViewById(R.id.wap_login1).setOnClickListener(this);
        findViewById(R.id.get_user_info).setOnClickListener(this);
        findViewById(R.id.pre_getphone).setOnClickListener(this);
        findViewById(R.id.validate_phone_bt).setOnClickListener(this);
        findViewById(R.id.network_btn).setOnClickListener(this);
        findViewById(R.id.wap_login_display).setOnClickListener(this);
        findViewById(R.id.wap_login_display_window).setOnClickListener(this);
        findViewById(R.id.del_scrip).setOnClickListener(this);
        TextView mVersionText = findViewById(R.id.text_version);
        mVersionText.setText(verifyHelper.mSDKVersion);
        phoneEt = findViewById(R.id.phone_et);
        mResultDialog = new ResultDialog(mContext);

    }

    @Override
    public void onClick(View v) {
//        isShowResult = true;
        String overTime = ((EditText) findViewById(R.id.over_time)).getText().toString();
        if (!TextUtils.isEmpty(overTime)) {
//            mGenAuthnHelper.setOverTime(Integer.valueOf(overTime));
            verifyHelper.authOverTime(Integer.valueOf(overTime));
        } else {
            verifyHelper.authOverTime(8000);
//            mGenAuthnHelper.setOverTime(8000);
        }
        int id = v.getId();
        /*if (id == R.id.get_user_info) {
            ssoHelper.testUserInfo();
        } else if (id == R.id.pre_getphone) {
            ssoHelper.PGWGetMobile(this);
        } else if (id == R.id.wap_login1) {
            ssoHelper.implicitLogin(this);
        } else if (id == R.id.validate_phone_bt) {
            ssoHelper.phoneValidate(phoneEt.getText().toString());
        } else*/ if (id == R.id.network_btn) {
            verifyHelper.getNetAndOperator();
        } else if (id == R.id.wap_login_display) {
            verifyHelper.tryAuthLogin(this);
        } else if (id == R.id.wap_login_display_window) {
            verifyHelper.tryAuthLoginByDialog(this);
        } else if (id == R.id.del_scrip) {
            verifyHelper.deleteScrip();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        SpUtils.putLong("phoneTimes", System.currentTimeMillis());
        switch (requestCode) {
//            case PERMISSIONS_REQUEST_READ_PHONE_STATE_PRE:
////                mGenAuthnHelper.getPhoneInfo(Constant.APP_ID, Constant.APP_KEY, mListener, CMCC_SDK_REQUEST_GET_PHONE_INFO_CODE);
//                ssoHelper.getMobile();
//                break;
//            case PERMISSIONS_REQUEST_READ_PHONE_STATE_IMPLICIT_LOGIN:
////                mGenAuthnHelper.mobileAuth(Constant.APP_ID, Constant.APP_KEY, mListener, CMCC_SDK_REQUEST_MOBILE_AUTH_CODE);
//                ssoHelper.mobileAuth();
//                break;
            case PERMISSIONS_REQUEST_READ_PHONE_STATE_DISPLAY_LOGIN:
//                mGenAuthnHelper.loginAuth(Constant.APP_ID, Constant.APP_KEY, mListener, CMCC_SDK_REQUEST_LOGIN_AUTH_CODE);
                verifyHelper.logAuth();
                break;
            default:
                break;
        }
    }

    Handler mHandler = new MyHandler(this);

    public static class MyHandler extends Handler {
        private WeakReference<SimpleActivity> referenceActivity;

        private MyHandler(SimpleActivity activity) {
            referenceActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            SimpleActivity mainActivity = referenceActivity.get();
            if (mainActivity != null && !mainActivity.isFinishing()) {
                switch (msg.what) {
                    case RESULT:
                        if(!TextUtils.isEmpty(referenceActivity.get().mResultString)) {
                            referenceActivity.get().mResultDialog.setResult(StringFormat
                                    .logcatFormat(referenceActivity.get().mResultString));
                        }
                        break;
                    case RESULT_OF_SIM_INFO:
                        referenceActivity.get().mResultDialog.setResult(referenceActivity.get().mResultString);
                    default:
                        break;
                }
            }
        }

    }

}
